package com.lavegue.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.lavegue.common.BaseHibernateDao;
import com.lavegue.common.Page;
import com.lavegue.dao.SysMessageDao;
import com.lavegue.model.SysMsg;
import com.lavegue.model.UserInfo;
import com.sun.org.apache.bcel.internal.generic.NEW;

public class SysMessageDaoImpl extends BaseHibernateDao<SysMsg, Integer> implements SysMessageDao {

	public SysMessageDaoImpl() {
		super(SysMsg.class);
	}
	
	
	/**
	 * ljp 20120112 查询所有发件人的消息
	 * 
	 * @param pageNo 当前页数
	 * @param pageSize 每页多少条
	 * @param userId 用户ID(接收者的ID)
	 * @return sysmsg对象
	 * @throws Exception
	 */
	public Page<SysMsg> queryAllSysMsgList(int pageNo, int pageSize,int userId){
		List<Criterion> conditions = new ArrayList<Criterion>();
		Order order = Order.desc("sendTime"); //发送时间降序
		
		if(userId != 0){  //接收者的用户ID
			conditions.add(Restrictions.eq("userInfo.id", Integer.valueOf(userId)));
		//  conditions.add(Restrictions.or(Restrictions.eq("userInfo.id", Integer.valueOf(userId)),Restrictions.isNull("userInfo.id")));
		}
		try {
			Page<SysMsg> sysMsgPage = this.findPage(pageNo, pageSize, conditions, order);
			return sysMsgPage;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * ljp 20120112  删除系统消息记录
	 * @param sysMsgId 系统消息ID
	 * @throws Exception
	 */
	public boolean deleteSysMsgById(SysMsg entity){
		try {
			this.getHibernateTemplate().delete(entity);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
		
	/**
	 * ljp 20120112  通过ID查询系统消息记录
	 * @param sysMsgId 系统消息ID
	 * @throws Exception
	 */
	public SysMsg querySysMsgById(Integer sysMsgId){
		String queryString = "from SysMsg where id=?";
		List<SysMsg> list = this.getHibernateTemplate().find(queryString,sysMsgId);
		if (!list.isEmpty()) {
			return (SysMsg) list.get(0);
		}
		return null;
	}
	
}
